<?php

namespace app\Api\controller;

use app\common\business\SMember;
use app\common\lib\Str;

class Login extends ApiBase
{
    //微信授权登录
    protected $wxLoginUrl;

    public function wxLogin()
    {
        $appid = $this->request->param("appid","","trim");
        if(empty($appid)){
            return show(config("status.error"),"appid不能为空");
        }
        $code = $this->request->param("code","","trim");
        if(empty($code)){
            return show(config("status.error"),"code不能为空");
        }
        $nickname = $this->request->param("nickname","","trim");
        if(empty($nickname)){
            return show(config("status.error"),"昵称不能为空");
        }
        $avatar_url = $this->request->param("avatar_url","","trim");
        if(empty($avatar_url)){
            return show(config("status.error"),"头像不能为空");
        }
        /*$encryptedData = $this->request->param("encryptedData","","trim");
        if(empty($encryptedData)){
            return show(config("status.error"),"加密数据不能为空(encryptedData)");
        }
        $iv = $this->request->param("iv","","trim");
        if(empty($iv)){
            return show(config("status.error"),"加密数据不能为空(iv)");
        }*/
        $wxAppID = config("wx.".$appid.".WECHAT_APPID");
        $wxAppSecret = config("wx.".$appid.".WECHAT_SECRET");
        $this->wxLoginUrl = sprintf(config("wx.LOGIN_URL"), $wxAppID, $wxAppSecret,$code);
        $result = (new Str())->vget($this->wxLoginUrl);
        $wxResult = json_decode($result, true);
        if(empty($wxResult)){
            return show(config("status.error"),"获取session_key及openID时异常,微信内部错误");
        }else{
            $loginFail = array_key_exists('errcode', $wxResult); // 使用 array_key_exists 更安全
            if($loginFail){
                return show(config("status.error"),"登录失败",$wxResult);
            }else{
                // 检查 unionid 是否存在
                if(!array_key_exists('unionid', $wxResult)){
                    return show(config("status.error"),"获取unionid时异常,微信内部错误");
                }
                // 解密小程序加密数据
                // (new Str())->wechatDecryptData($encryptedData,$iv,$wxResult['session_key']);
                try {
                    $data = [
                        'openid'        => $wxResult['openid'],
                        'unionid'       => $wxResult['unionid'],
                        'nickname'      => $nickname,
                        'avatar_url'    => $avatar_url,
                        'appid'         => $appid,
                    ];
                    $SMember = new SMember();
                    $result = $SMember->login($data);
                }catch (\Exception $e){
                    return show(config("status.error"),$e->getMessage());
                }
                if($result){
                    return show(config("status.success"),"登录成功",$result);
                }else{
                    return show(config("status.error"),"登录失败");
                }
            }
        }
    }
}
